package com.xnfh.framework.base

import android.util.Log
import androidx.lifecycle.Observer
import com.xnfh.framework.bean.BaseResp

/**
 * Created by stew on 8/28/22.
 * mail: stewforani@gmail.com
 */
open class BaseStateObserver<T>(var t: Boolean? = false) : Observer<BaseResp<T>> {

    override fun onChanged(t: BaseResp<T>) {

        when (t.responseState) {
            BaseResp.ResponseState.REQUEST_START -> {
                Log.d("BaseStateObserver", "Observer: start")
                getRespDataStart()
            }

            BaseResp.ResponseState.REQUEST_SUCCESS -> {
                Log.d("BaseStateObserver", "Observer: success")
                if (t.data == null && t.result == null && t.SfjData == null) {
                    getRespSuccess()
                } else {
                    if(t.data != null){
                        getRespDataSuccess(t.data!!)
                    }else if(t.result != null){
                        getRespDataSuccess(t.result!!)
                    }else{
                        getRespDataSuccess(t.SfjData!!)
                    }
                }

            }

            BaseResp.ResponseState.REQUEST_FAILED -> {
                Log.d("BaseStateObserver", "Observer: failed")
                getRespDataError()
            }

            BaseResp.ResponseState.REQUEST_ERROR -> {
                Log.d("BaseStateObserver", "Observer: error")
                getRespDataError()
            }

            else -> {
                getRespDataError()
            }
        }
    }

    open fun getRespDataStart() {}
    open fun getRespDataSuccess(it: T) {}
    open fun getRespSuccess() {}
    open fun getRespDataError() {}
}